#include "spi.h"
Spi_handle_def spi0_handle = {
    .CPHA = SPI_CPHA_2EDGE,
    .CPOL = SPI_CPOL_HIGH,
    .Prescaler = 4,
    .instance = SPI0,
    .FirstBit = SPI_FIRST_BIT_MSB,
    .MODE = SPI_MODE_MASTER,
    .NSS = SPI_NSS_SOFT, /*TCR BIT6=1 BIT2=1  BIT7 控制CS引脚*/
};

void spi0_init(void)
{
    /* pc0-clk  pc1-cs  pc2-miso   pc3-mosi */
    gpio_init(GPIOC, PIN0 | PIN1 | PIN2 | PIN3, GPIO_MODE_AF2, GPIO_PULL_NONE, GPIO_DRV_3);
    _SPI0_CLK_ENABLE();
    Spi_Init(&spi0_handle);
}

void spi0_cs(uint8_t state)
{
    Spi_Soft_CS(&spi0_handle, state);
}

void spi0_write_and_read(uint8_t *tx, uint8_t *rx, uint32_t length)
{
    Spi_Tranmit(&spi0_handle, tx, rx, length);
}
